package com.theobserver.theobserver;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;

public class SignInActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signin);



        Button bt = (Button) findViewById(R.id.btn_Register_ok);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String name = ((EditText) findViewById(R.id.Reg_username)).getText().toString();


                Sensitivewords sen = new Sensitivewords();

                //int tmp = 0;

                for (int i=0;i< sen.str.length;i++){
                    if (name.matches(sen.str[i])){
                        Toast.makeText(SignInActivity.this, "含有不合乎规范的词汇，请重新输入！", Toast.LENGTH_SHORT).show();
                        //tmp = 1;
                        return;
                    }
                }


                String pwd = ((EditText) findViewById(R.id.Reg_Pwd)).getText().toString();
                String pwdagain = ((EditText) findViewById(R.id.Reg_PwdAgain)).getText().toString();
                String email = ((EditText) findViewById(R.id.Reg_Mail)).getText().toString();

                AVUser user = new AVUser();// 新建 AVUser 对象实例
                user.setUsername(email);// 设置用户名，将邮箱账号设为默认用户名，不可重复
                user.setPassword(pwd);// 设置密码
                user.setEmail(email);// 设置邮箱

                user.put("user_name",name);//自己新加的属性，设置个性昵称，可以重复


                if (name.length() != 0) {
                    if (pwd.length() != 0) {
                        if (pwd.equals(pwdagain)) {
                            if (email.length() != 0) {
                                user.signUpInBackground(new SignUpCallback() {
                                    @Override
                                    public void done(AVException e) {
                                        if (e == null) {
                                            Toast.makeText(SignInActivity.this, "注册成功，请前往邮箱进行验证！", Toast.LENGTH_SHORT).show();
                                            finish();
                                        } else if (e.getCode() == 203) {
                                            Toast.makeText(SignInActivity.this, "此邮箱已注册！", Toast.LENGTH_SHORT).show();
                                        }
                                        else
                                            Toast.makeText(SignInActivity.this, toString(), Toast.LENGTH_SHORT).show();
                                    }
                                });
                            } else
                                Toast.makeText(SignInActivity.this, "请输入邮箱账号！", Toast.LENGTH_SHORT).show();
                        } else
                            Toast.makeText(SignInActivity.this, "两次输入的密码不一样,请重新设置密码！", Toast.LENGTH_SHORT).show();
                    } else
                        Toast.makeText(SignInActivity.this, "请输入密码！", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(SignInActivity.this, "请输入用户名！", Toast.LENGTH_SHORT).show();

            }
        });

        //返回
        TextView Tv = (TextView) findViewById(R.id.tv_Reg_reten);
        Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
}
